package com.junqson.je.msg.repo;

import com.junqson.je.msg.model.MessageModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/2/10 18:01
 * @Version 1.0
 */
@Repository
public interface MessageRepo extends JpaRepository<MessageModel, Long> {

    /**
     * 通过toId查找接收的站内信
     * @param toId
     * @return
     */
    List<MessageModel> findByToIdOrderByCreateTimeDesc(Integer toId);

    /**
     * 通过fromId查找发送的站内信
     */
    List<MessageModel> findByFromId(Integer fromId);

    /**
     * 设置状态为已读
     * @param id
     */
    @Modifying
    @Transactional
    @Query("update MessageModel m set m.readState=true where m.msgId=?1 and m.toId=?2")
    void read(Long id, Integer userId);

    /**
     * 查找收到的未读通知
     * @param fromId
     * @param readState
     * @return
     */
    List<MessageModel> findByToIdAndReadState(Integer fromId, Boolean readState);
}
